package com.yonyou.pmclouds.basecom.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.iuap.tenant.sdk.RoleAppCenter;
import com.yonyou.iuap.tenant.sdk.UserRoleCenter;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
public class SDKUtil {

    public static Set<String> findYhtAuth()
    {
        ObjectMapper jsonMapper = new ObjectMapper();
        Set<String> permissions = new HashSet<String>();
        String auth = UserRoleCenter.queryRoleByUser(RuntimeEnvironment.getLoginUserId(),RuntimeEnvironment.getTenantId(),RuntimeEnvironment.getSysId());
        Map<String, ?> rolemap = null;
        Map<String, ?> promap = null;
        List<String> li = null;
        try {
            if(auth!=null)
            {
                rolemap = jsonMapper.readValue(auth, Map.class);
                li = (List) rolemap.get("data");
                for ( String str: li) {
                    String pro = RoleAppCenter.getAppCode(str, "1");
                    promap = jsonMapper.readValue(pro, Map.class);
                    List list = (List) promap.get("data");
                    permissions.addAll(list);
                }
            }
        } catch (IOException e) {
            log.error("查询功能权限错误");
        }
        return permissions;
    }
}
